/************************************************************************
 * @file StandardPluginListDialog.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <pluginui/widget/StandardPluginListDialog.h>
#include "ui_StandardPluginListDialog.h"
#include <plugin/util/Framework.h>
#include <plugin/plugin/PluginDescription.h>
#include <plugin/util/PluginDescriptionManager.h>

namespace antu
{
namespace plugin
{
namespace ui
{
StandardPluginListDialog::StandardPluginListDialog(QWidget *parent) :
    QDialog(parent),
    ui(new ::Ui::StandardPluginListDialog)
{
    ui->setupUi(this);

    this->setWindowIcon(QIcon(":/resources/images/plugin_list.png"));

    vector<PluginDescriptionSharedPtr> descriptions = antu::plugin::Framework::Instance.descriptionManager()->findAll();

    size_t size = descriptions.size();
    for (size_t i = 0; i < size; i++)
    {
        this->ui->listWidget->addItem(descriptions[i]->name());
    }

    this->connect(this->ui->closePushButton, SIGNAL(clicked()), this, SLOT(closeClicked()));
    this->connect(this->ui->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));

    this->ui->codeLineEdit->setEnabled(false);
    this->ui->nameLineEdit->setEnabled(false);
    this->ui->versionLineEdit->setEnabled(false);
    this->ui->compatibleVersionLineEdit->setEnabled(false);
    this->ui->pathLineEdit->setEnabled(false);
    this->ui->startLevelLineEdit->setEnabled(false);

    if (descriptions.size() > 0)
        this->ui->listWidget->setCurrentRow(0);
}

StandardPluginListDialog::~StandardPluginListDialog()
{
    delete ui;
}

void StandardPluginListDialog::currentRowChanged(int row)
{
    vector<PluginDescriptionSharedPtr> descriptions = antu::plugin::Framework::Instance.descriptionManager()->findAll();
    if (row >= 0 && row < descriptions.size())
    {
        antu::plugin::PluginDescriptionSharedPtr description = descriptions[row];

        this->ui->codeLineEdit->setText(description->code());
        this->ui->nameLineEdit->setText(description->name());
        this->ui->versionLineEdit->setText(description->version().toString());
        this->ui->compatibleVersionLineEdit->setText(description->minimumCompatibleVersion().toString());
        this->ui->pathLineEdit->setText(description->path());
        this->ui->startLevelLineEdit->setText(QString("%1").arg(description->startLevel()));

        this->ui->requirementsTreeWidget->clear();
        size_t requirementCount = description->requirementCount();
        for (size_t i = 0; i < requirementCount; i++)
        {
            QStringList content;
            content << description->requirementAt(i).code();
            content << description->requirementAt(i).requiredVersion().toString();
            QTreeWidgetItem* pItem = new QTreeWidgetItem(this->ui->requirementsTreeWidget, content);
            this->ui->requirementsTreeWidget->addTopLevelItem(pItem);
        }
    }
}

void StandardPluginListDialog::closeClicked()
{
    this->close();
}

}
}
}

